<template>
  <div class="addform">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="80px"
      class="demo-ruleForm"
    >
      <el-form-item label="输入文字" prop="twText">
        <el-input v-model="ruleForm.twText"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">
          修改打字机文字
        </el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {},
      rules: {//如果没输入的提示信息
        twText: [
          { required: true, message: "请输入打字机动画的文字", trigger: "blur" },
          { min: 1, max: 20, message: "最大长度5个字,不能低于1个字", trigger: "blur" },
        ]
      },
    };
  },
  methods: {
    //调用数据并且执行
    assignData(id) {
      console.log("传过来" + id);
      const _this = this;
      //打开页面向后端请求显示全部数据
      this.axios({
        method: "get",
        // 公共变量baseUrl() 已经定义在main.js.用于修改后端端口
        url: _this.baseUrl + "/index/get/indexTw/" + id,
      }).then(function (resp) {
        console.log("获取数据成功");
        _this.ruleForm = resp.data;
      });
    },
    submitForm(formName) {
      // 提交方法 先判断提交表单是否有空=>validate
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log("进入修改");
          // 由于axios内部必须使用_this才能使用this
          const _this = this;
          this.axios({
            method: "post",
            url: _this.baseUrl + "/index/post/editTw",
            //传递给后端的数据
            data: _this.ruleForm
          }).then(function (resp) {
            console.log(resp.data);
            _this.$parent.$parent.successEdit();
          });
        } else {
          console.log("修改失败");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.addform {
  width: 100%;
  margin: 0 auto;
}
</style>